package org.deadline.model.config;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import org.deadline.model.baseapp.OrganizationDataOnDemand;
import org.deadline.model.user.PersonDataOnDemand;
import org.deadline.repository.config.CustomFormElementRepository;
import org.deadline.service.config.CustomFormElementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Component;

@Configurable
@Component
public class CustomFormElementDataOnDemand {

	private Random rnd = new SecureRandom();

	private List<CustomFormElement> data;

	@Autowired
    private OrganizationDataOnDemand organizationDataOnDemand;

	@Autowired
    private PersonDataOnDemand personDataOnDemand;

	@Autowired
    private EnumerationDataOnDemand enumerationDataOnDemand;

	@Autowired
    CustomFormElementService customFormElementService;

	@Autowired
    CustomFormElementRepository customFormElementRepository;

	public CustomFormElement getNewTransientCustomFormElement(int index) {
        CustomFormElement obj = new CustomFormElement();
        setControlType(obj, index);
        setCreationDate(obj, index);
        setDefaultconfigValues(obj, index);
        setDeleted(obj, index);
        setDescription(obj, index);
        setDisplayed(obj, index);
        setEnabled(obj, index);
        setModificationDate(obj, index);
        setOptionconfigValuess(obj, index);
        setRemember(obj, index);
        setRequired(obj, index);
        setScreenType(obj, index);
        setUiLabel(obj, index);
        setUiOrder(obj, index);
        setUiconfigKey(obj, index);
        return obj;
    }

	public void setControlType(CustomFormElement obj, int index) {
        String controlType = "controlType_" + index;
        if (controlType.length() > 100) {
            controlType = controlType.substring(0, 100);
        }
        obj.setControlType(controlType);
    }

	public void setCreationDate(CustomFormElement obj, int index) {
        Date creationDate = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setCreationDate(creationDate);
    }

	public void setDefaultconfigValues(CustomFormElement obj, int index) {
        String defaultconfigValues = "defaultconfigValues_" + index;
        if (defaultconfigValues.length() > 100) {
            defaultconfigValues = defaultconfigValues.substring(0, 100);
        }
        obj.setDefaultconfigValues(defaultconfigValues);
    }

	public void setDeleted(CustomFormElement obj, int index) {
        Boolean deleted = true;
        obj.setDeleted(deleted);
    }

	public void setDescription(CustomFormElement obj, int index) {
        String description = "description_" + index;
        if (description.length() > 1000) {
            description = description.substring(0, 1000);
        }
        obj.setDescription(description);
    }

	public void setDisplayed(CustomFormElement obj, int index) {
        Boolean displayed = true;
        obj.setDisplayed(displayed);
    }

	public void setEnabled(CustomFormElement obj, int index) {
        Boolean enabled = true;
        obj.setEnabled(enabled);
    }

	public void setModificationDate(CustomFormElement obj, int index) {
        Date modificationDate = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setModificationDate(modificationDate);
    }

	public void setOptionconfigValuess(CustomFormElement obj, int index) {
        String optionconfigValuess = "optionconfigValuess_" + index;
        if (optionconfigValuess.length() > 100) {
            optionconfigValuess = optionconfigValuess.substring(0, 100);
        }
        obj.setOptionconfigValuess(optionconfigValuess);
    }

	public void setRemember(CustomFormElement obj, int index) {
        Boolean remember = true;
        obj.setRemember(remember);
    }

	public void setRequired(CustomFormElement obj, int index) {
        Boolean required = true;
        obj.setRequired(required);
    }

	public void setScreenType(CustomFormElement obj, int index) {
        String screenType = "screenType_" + index;
        if (screenType.length() > 255) {
            screenType = screenType.substring(0, 255);
        }
        obj.setScreenType(screenType);
    }

	public void setUiLabel(CustomFormElement obj, int index) {
        String uiLabel = "uiLabel_" + index;
        if (uiLabel.length() > 100) {
            uiLabel = uiLabel.substring(0, 100);
        }
        obj.setUiLabel(uiLabel);
    }

	public void setUiOrder(CustomFormElement obj, int index) {
        Integer uiOrder = new Integer(index);
        obj.setUiOrder(uiOrder);
    }

	public void setUiconfigKey(CustomFormElement obj, int index) {
        String uiconfigKey = "uiconfigKey_" + index;
        if (uiconfigKey.length() > 100) {
            uiconfigKey = uiconfigKey.substring(0, 100);
        }
        obj.setUiconfigKey(uiconfigKey);
    }

	public CustomFormElement getSpecificCustomFormElement(int index) {
        init();
        if (index < 0) {
            index = 0;
        }
        if (index > (data.size() - 1)) {
            index = data.size() - 1;
        }
        CustomFormElement obj = data.get(index);
        Long id = obj.getId();
        return customFormElementService.findCustomFormElement(id);
    }

	public CustomFormElement getRandomCustomFormElement() {
        init();
        CustomFormElement obj = data.get(rnd.nextInt(data.size()));
        Long id = obj.getId();
        return customFormElementService.findCustomFormElement(id);
    }

	public boolean modifyCustomFormElement(CustomFormElement obj) {
        return false;
    }

	public void init() {
        int from = 0;
        int to = 10;
        data = customFormElementService.findCustomFormElementEntries(from, to);
        if (data == null) {
            throw new IllegalStateException("Find entries implementation for 'CustomFormElement' illegally returned null");
        }
        if (!data.isEmpty()) {
            return;
        }
        
        data = new ArrayList<CustomFormElement>();
        for (int i = 0; i < 10; i++) {
            CustomFormElement obj = getNewTransientCustomFormElement(i);
            try {
                customFormElementService.saveCustomFormElement(obj);
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> iter = e.getConstraintViolations().iterator(); iter.hasNext();) {
                    ConstraintViolation<?> cv = iter.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            customFormElementRepository.flush();
            data.add(obj);
        }
    }
}
